//
//  xsConfigurationMusicCollectionViewCell.m
//  digital_robot_demo
//
//  Created by xskj on 2024/8/30.
//

#import "xsConfigurationMusicCollectionViewCell.h"
  

@interface xsConfigurationMusicCollectionViewCell()

@property (nonatomic,strong) UILabel *titleLabel;
@property (nonatomic,strong) UIView *bottomView;
@property (nonatomic,strong) UIImageView *iconImageView;

@end

@implementation xsConfigurationMusicCollectionViewCell


- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    
    if(self){
        [self.contentView addSubview:self.bottomView];
        [self.bottomView addSubview:self.iconImageView];
        [self.bottomView addSubview:self.titleLabel];
        
        
        [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        
        [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.bottomView);
        }];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self.bottomView);
        }];
    }
    return self;
}

- (void)setModel:(xsConfigurationMusicCollectionViewCellModel *)model
{
    _model = model;
    
    
    if(!kStringIsEmpty(model.name)){
        self.titleLabel.text = model.name;
    }
    else{
        self.titleLabel.text = @"";
    }
    XSWeakSelf;
    //audioType;// 0我的 1官方
    if(model.audioType==0){
        if(!kStringIsEmpty(model.previewImage)){
            
//            [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:model.previewImage] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
//                
//            }];
            
            
            NSData *urlImageData =  [[SDImageCache sharedImageCache]diskImageDataForKey:model.previewImage];
            if(urlImageData){
                weakSelf.iconImageView.image = [UIImage imageWithData:urlImageData];
            }
            else{
                [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:model.previewImage] options:SDWebImageDownloaderLowPriority progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
                    
                } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
                        if (image) {
                            [[SDImageCache sharedImageCache] storeImageDataToDisk:data forKey:model.previewImage];
                            
                            // 图片下载成功，将其设置到UIImageView中
                            dispatch_async(dispatch_get_main_queue(), ^{
                                weakSelf.iconImageView.image = image;
                            });
                        } else {
                            // 处理错误
                            NSLog(@"Error downloading image: %@", error);
                        }
                }];
            }
        }
        
     

    }
    else{
        if(!kStringIsEmpty(model.img)){
            
//            [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:model.img] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
//                
//            }];
//            
            
            NSData *urlImageData =  [[SDImageCache sharedImageCache]diskImageDataForKey:model.img];
            if(urlImageData){
                weakSelf.iconImageView.image = [UIImage imageWithData:urlImageData];
            }
            else{
                [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:model.img] options:SDWebImageDownloaderLowPriority progress:^(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL) {
                    
                } completed:^(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished) {
                        if (image) {
                            [[SDImageCache sharedImageCache] storeImageDataToDisk:data forKey:model.img];
                            
                            // 图片下载成功，将其设置到UIImageView中
                            dispatch_async(dispatch_get_main_queue(), ^{
                                weakSelf.iconImageView.image = image;
                            });
                        } else {
                            // 处理错误
                            NSLog(@"Error downloading image: %@", error);
                        }
                }];
            }
        }
    }
    
    
    if(!model.isBeClick){
        self.bottomView.layer.cornerRadius = 4.0;
        self.bottomView.layer.borderWidth = 0.0;
        self.bottomView.layer.borderColor = (UIColor.clearColor).CGColor;
    }
    else{
        
        self.bottomView.layer.cornerRadius = 4.0;
        self.bottomView.layer.borderWidth = 1.0;
        self.bottomView.layer.borderColor = COLOR_Blue.CGColor;
    }
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
- (UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[UIView alloc] init];
        _bottomView.backgroundColor = UIColor.clearColor;//COLOR_VCViewBgcolor;
    }
    return _bottomView;
}
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.backgroundColor = UIColor.clearColor;
        _titleLabel.font = [UIFont systemFontOfSize:14];//FontSemibold(17);
        _titleLabel.textColor = COLOR_textWhiteColor;//COLOR_TextGray9B;
        _titleLabel.numberOfLines = 3;
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}



- (UIImageView *)iconImageView{
    if (!_iconImageView) {
        _iconImageView = [[UIImageView alloc] init];
        ////        _imageView.layer.masksToBounds = YES;
        ////        _imageView.layer.cornerRadius  = 2.f;
        ////        _imageView.layer.borderWidth = 1.0f;
        ////        _imageView.layer.borderColor = [HexRGB(0x444444, 1.0f) CGColor];
        ////        [_imageView setTitleColor:HexRGB(0x444444, 1.0f) forState:UIControlStateNormal];
        ////        [_imageView.titleLabel setFont:FontMedium(14.f)];
        ////        [_imageView addTarget:self action:@selector(otherAction) forControlEvents:UIControlEventTouchUpInside];
        //        [_imageView setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
        //        [_imageView setImage:[UIImage imageNamed:@"editorFontSizeSelect"] forState:UIControlStateSelected];
        //        _iconimageView.image = [UIImage imageNamed:@"BottomSelectCellViewSelectImage"];
        _iconImageView.backgroundColor = UIColor.clearColor;
        _iconImageView.contentMode = UIViewContentModeScaleAspectFill;
        //        _iconimageView.hidden = NO;
    }
    return _iconImageView;
}

@end
